package org.openkanban.client;

import org.openkanban.client.cometd.CometClient;
import org.openkanban.client.ui.adminpanel.AdminPanel;
import org.openkanban.client.ui.board.BoardView;
import org.openkanban.client.ui.board.WelcomeView;
import org.openkanban.client.ui.footer.Footer;
import org.openkanban.client.util.RootPanel;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define {@code onModuleLoad()}. Initializes the
 * application.
 */
public class Main implements EntryPoint {

    public static final int DEFAULT_FADE_DURATION = 400;

    private Layout layout;
    private AdminPanel adminPanel;
    private BoardView board;
    private WelcomeView welcome;
    private Footer footer;
    private VLayout vlMain = new VLayout();

    private CometClient cometClient;

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
	initMain();
	vlMain.animateShow(AnimationEffect.FADE, null, 1000);
    }

    private void initMain() {
	RootPanel.main = this;
	Page.setAppImgDir("[APP]/images/");

	vlMain = new VLayout();

	layout = new VLayout();
	layout.setWidth100();

	adminPanel = new AdminPanel();
	welcome = new WelcomeView();

	vlMain.setSmoothFade(true);
	vlMain.setWidth100();
	vlMain.setHeight100();
	vlMain.setVisible(false);

	layout.addMember(welcome);
	vlMain.addResizedHandler(new ResizedHandler() {
	    @Override
	    public void onResized(ResizedEvent event) {
		if (footer != null) {
		    footer.refreshTopPosition();
		}
	    }
	});

	vlMain.addMember(adminPanel);
	vlMain.addMember(layout);
	HLayout placeHolder = new HLayout();
	placeHolder.setHeight(20);
	vlMain.addMember(placeHolder);

	vlMain.setOverflow(Overflow.HIDDEN);

	vlMain.draw();

	cometClient = new CometClient();
    }

    public Layout getLayout() {
	return layout;
    }

    public AdminPanel getAdminPanel() {
	return adminPanel;
    }

    public BoardView getBoard() {
	return board;
    }

    public void setBoard(BoardView board) {
	this.board = board;
    }

    public Footer getFooter() {
	return footer;
    }

    public void setFooter(Footer footer) {
	this.footer = footer;
    }

    public VLayout getVlMain() {
	return vlMain;
    }

    public WelcomeView getWelcome() {
	return welcome;
    }

    public CometClient getCometClient() {
	return cometClient;
    }

}
